package com.ctg.payroll2.dao.payment.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.ctg.payroll2.bean.common.ColumnModel;
import com.ctg.payroll2.bean.filter.payment.PayClazzItemFilter;
import com.ctg.payroll2.bean.po.payment.PayClassitem;
import com.ctg.payroll2.bean.po.payment.PayClassitemLimitDate;
import com.ctg.payroll2.dao.payment.PayClazzItemDao;
import com.ctg.platform.orm.ibatis.IbatisEntityDao;
import com.ctg.platform.util.Page;

@SuppressWarnings("unchecked")
public class PayClazzItemDaoImpl extends IbatisEntityDao implements PayClazzItemDao{
	@SuppressWarnings("unchecked")
	public List<ColumnModel> getColumnModel(Long payClassId) {
		List<ColumnModel> list = new ArrayList<ColumnModel>();
		try {
			list = getSqlMapClient().queryForList(getNameSpace() 
					+ ".queryColumnModel_query", payClassId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public List<ColumnModel> getColumnModelHis(PayClazzItemFilter filter) {
		List<ColumnModel> list = new ArrayList<ColumnModel>();
		try {
			list = getSqlMapClient().queryForList(getNameSpace() 
					+ ".queryColumnModel_his_query", filter);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	/*
	 * (non-Javadoc)
	 * @see com.ctg.payroll2.dao.payment.PayClazzItemDao#getPayItemListByFilter(com.ctg.payroll2.bean.filter.payment.PayClazzItemFilter)
	 */
	public Page getPayItemListByFilter(PayClazzItemFilter filter){
		return pagedQueryByFilter(this.getClass(), filter,
				".queryPayClazzItem_count",
				".queryPayClazzItem_query");
	}
	/*
	 * (non-Javadoc)
	 * @see com.ctg.payroll2.dao.payment.PayClazzItemDao#getPayItemListByFilter(com.ctg.payroll2.bean.filter.payment.PayClazzItemFilter)
	 */
	public List<PayClassitem> getDisplayPayItemListByFilter(PayClazzItemFilter filter) {
		try {
			return getSqlMapClient().queryForList(getNameSpace() + ".queryDisplayPayClazzItem_query", filter);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return new ArrayList();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.ctg.payroll2.dao.payment.PayClazzItemDao#deletePayItemByIds(java.lang.String)
	 */
	public int deletePayItemByIds(String ids) throws SQLException{
		return getSqlMapClient().delete(getNameSpace() + ".deletePayItemByIds", ids);
	}
	public int fakeDeletePayItemByIds(String ids) throws SQLException{
		return getSqlMapClient().delete(getNameSpace() + ".fakeDeletePayItemByIds", ids);
	}
	/*
	 * (non-Javadoc)
	 * @see com.ctg.payroll2.dao.payment.PayClazzItemDao#insertPayItem(com.ctg.payroll2.bean.po.payment.PayClassitem)
	 */
	public Long insertPayItem(PayClassitem payItem){
		Long l = new Long(0);
		try {
			l = (Long)getSqlMapClient().insert(getNameSpace() + ".insertPayItem", payItem);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return l;
		
	}
	
	@SuppressWarnings("unchecked")
	public List<PayClassitem> queryPayItemByFilter(PayClazzItemFilter filter){
		List<PayClassitem> list = new ArrayList<PayClassitem>();
		try {
			list = getSqlMapClient().queryForList(getNameSpace() + ".queryPayItemByFilter", filter);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	public int validateValidAndDisplay(PayClassitem payItem){
		int i = 0;
		try {
			i = (Integer)getSqlMapClient().queryForObject(getNameSpace() + ".validateValidAndDisplay", payItem);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	public List<Integer> getNextSegmentOrder(PayClassitem payItem){
		List<Integer> list = null;
		try {
			list = getSqlMapClient().queryForList(getNameSpace() + ".getNextSegmentOrder", payItem);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	public int modifyPayItem(PayClassitem payItem){
		int i = 0;
		try {
			i = getSqlMapClient().update(getNameSpace() + ".modifyPayItem", payItem);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	public int modifyPayItemAll(PayClassitem payItem){
		int i = 0;
		try {
			i = getSqlMapClient().update(getNameSpace() + ".modifyPayItemAll", payItem);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	@SuppressWarnings("unchecked")
	public List<PayClassitemLimitDate> getTimeLimitByPayItemId(Long id){
		List<PayClassitemLimitDate> list = new ArrayList<PayClassitemLimitDate>();
		try {
			list = getSqlMapClient().queryForList(getNameSpace() + ".getTimeLimitByPayItemId", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	public Long insertTimeLimit(PayClassitemLimitDate limitTime){
		Long l = new Long(0);
		try {
			l = (Long)getSqlMapClient().insert(getNameSpace() + ".insertTimeLimit", limitTime);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return l;
	}
	
	public int deleteTimeLimitByIds(String ids){
		int i = 0;
		try {
			i = getSqlMapClient().delete(getNameSpace() + ".deleteTimeLimitByIds", ids);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	public int updateTimeLimit(PayClassitemLimitDate limitTime){
		int i = 0;
		try {
			i = getSqlMapClient().update(getNameSpace() + ".updateTimeLimit", limitTime);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	public String checkExistsInHis(String ids) throws SQLException{
		if(ids==null)
			return null;
		String ret = null;
		List<String> list = getSqlMapClient().queryForList(getNameSpace() + ".checkExistsInHis", ids);
		if (list.size()>0) {
			StringBuffer sb = new StringBuffer();
			for (String string : list) {
				sb.append(string).append(",");
			}
			ret = sb.substring(0, sb.length()-1);
		}
		return ret;
	}
	
	public String checkExistsInFormula(String ids) throws SQLException{
		if(ids==null)
			return null;
		String ret = null;
		List<String> list = getSqlMapClient().queryForList(getNameSpace() + ".checkExistsInFormula", ids);
		if (list.size()>0) {
			StringBuffer sb = new StringBuffer();
			for (String string : list) {
				sb.append(string).append(",");
			}
			ret = sb.substring(0, sb.length()-1);
		}
		return ret;
	}
	
	public List<PayClassitem> queryForFormula(PayClassitem payClassitem) {
		try {
			return getSqlMapClient().queryForList(getNameSpace() + ".queryForFormula", payClassitem);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return new ArrayList();
	}
	
	public String checkItemIsValid(PayClazzItemFilter filter) throws SQLException{
		return (String)getSqlMapClient().queryForObject(getNameSpace() + ".checkItemIsValid", filter);
	}
	
	private String getNameSpace(){
		return this.getClass().getSimpleName();
	}
}
